package cn.etgps.etgps.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import cn.etgps.etgps.R;
import cn.etgps.etgps.utils.MainUsedInterface;


public class CategorySelectView extends LinearLayout implements AdapterView.OnItemClickListener {
    MainUsedInterface.MainButtonClickListener mMainButtonListener = null;//点击监听
    private Context mContext = null;
    private ListView mCategoryList = null;
    private String[] mData = null;
    private ViewHolder mHolder = null;
    private String mCheckedName = "";
    private CategoryAdapter mAdapter = null;
    private LayoutInflater mInflater = null;
    private int mSelectItemIndex = -1;

    public CategorySelectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUI(context);
    }

    public CategorySelectView(Context context) {
        super(context);
        initUI(context);
    }

    //初始化布局、控件及item点击监听事件
    private void initUI(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        View view = mInflater.inflate(R.layout.choose_category_list_view, null);
        this.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mCategoryList = (ListView) view.findViewById(R.id.list);
        mCategoryList.setOnItemClickListener(this);
    }

    public void initCategoryList(String[] categories, String defaultCheckName,
            MainUsedInterface.MainButtonClickListener listener) {
        mMainButtonListener = listener;
        mAdapter = new CategoryAdapter();
        mData = categories;
        mCategoryList.setAdapter(mAdapter);
        if (defaultCheckName.equals("")) {
            mCheckedName = categories[0];
        } else {
            mCheckedName = defaultCheckName;
        }
    }

    public int getSelectedIndex() {
        return mSelectItemIndex;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (!mCheckedName.equals(mData[position])) {
            mAdapter.notifyDataSetChanged();
            mCheckedName = mData[position];
            if (mMainButtonListener != null) {
                mSelectItemIndex = position;
                mMainButtonListener.clickButtonListener(mData[position]);
            }
        }

    }

    static class ViewHolder {
        TextView isCheck;
        TextView name;
    }

    class CategoryAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData.length;
        }

        @Override
        public Object getItem(int position) {
            return mData[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.choose_category_list_item_view, null);
                mHolder = new ViewHolder();
                mHolder.name = (TextView) convertView.findViewById(R.id.category_name);
                mHolder.isCheck = (TextView) convertView.findViewById(R.id.category_check);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ViewHolder) convertView.getTag();
            }
            if (mCheckedName.equals(mData[position])) {
                mHolder.name.setTextColor(mContext.getResources().getColor(R.color.light_red_color));
                mHolder.isCheck.setVisibility(View.VISIBLE);
            } else {
                mHolder.name.setTextColor(mContext.getResources().getColor(R.color.dark_gray_color));
                mHolder.isCheck.setVisibility(View.GONE);
            }
            mHolder.name.setText(mData[position]);
            return convertView;
        }
    }
}
